using System;
using System.Diagnostics.CodeAnalysis;

namespace OpenWaves
{
    public sealed class FakeTimeScope : ITimeService, IDisposable
    {
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        public static FakeTimeScope Begin(DateTime time)
        {
            var fake = new FakeTimeScope { Now = time };
            fake.resolver = ServiceLocator.SetScopedResolver(new BasicResolver().Register<ITimeService>(fake));
            return fake;
        }

        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        public static FakeTimeScope Begin()
        {
            return Begin(Time.Now);
        }

        private IDisposable resolver;

        public DateTime Now { get; set; }

        public void Dispose()
        {
            this.resolver.Dispose();
        }
    }

    public static class FakeTimeScopeExtensions
    {
        public static void Forward(this FakeTimeScope scope, TimeSpan timeSpan)
        {
            scope.Now = scope.Now.Add(timeSpan);
        }
    }
}